package com.mjlf.shiro.login;

import com.mjlf.shiro.realm.MyRealm;
import com.mjlf.shiro.realm.MyRealm1;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.authz.permission.WildcardPermissionResolver;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler;
import org.apache.shiro.session.mgt.SessionValidationScheduler;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.web.servlet.FilterRegistrationBean;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Filter;

/**
 * @ClassName Login
 * @auther mjlf
 * @Date 2019/6/10 14:00
 * @Description TODO
 */
public class SecurityManagerDefault {

    @Test
    public void testLogin(){


        //2.从SecurityManagerFactory工具类中获取SecurityManager实例
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        //认证器
        ModularRealmAuthenticator modularRealmAuthenticator = new ModularRealmAuthenticator();
        modularRealmAuthenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());

        securityManager.setAuthenticator(modularRealmAuthenticator);

        //授权器
        ModularRealmAuthorizer modularRealmAuthorizer = new ModularRealmAuthorizer();
        modularRealmAuthorizer.setPermissionResolver(new WildcardPermissionResolver());
        securityManager.setAuthorizer(modularRealmAuthorizer);

        AuthorizingRealm realm = new MRealm();
        realm.setCredentialsMatcher(new ShiroCredentialsMatcher());
        securityManager.setRealm(realm);

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

        PathMatchingFilterChainResolver filterChainResolver = new PathMatchingFilterChainResolver();
        //2、创建 FilterChainManager
        DefaultFilterChainManager filterChainManager = new DefaultFilterChainManager();
        Map<String, Filter> filterMap = new HashMap<>();
//        filterChainManager.setFilters(filterMap);
        filterChainResolver.setFilterChainManager(filterChainManager);


        //3. 通过SecurityUtils设置securityManager
        SecurityUtils.setSecurityManager(securityManager);

        //4. 获取当前主体
        Subject subject = SecurityUtils.getSubject();

        //5.实例化登录认证token,此处使用账号/密码方式
        AuthenticationToken authenticationToken = new UsernamePasswordToken("zhang", "123");

        //6. 执行登录
        subject.login(authenticationToken);

        subject.getSession().touch();


        //7. 验证登录
        Assert.assertEquals(true, subject.isAuthenticated());

        //8. 退出登录
        subject.logout();
    }
}
